/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vehiculo.DAO;

import com.vehiculo.Entity.Cambio_Aceite;
import com.vehiculo.Entity.Vehiculo;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Daniel
 */
public class VehiculoDAO {
    
    private EntityManagerFactory emf;
    private EntityManager em;

    
    public VehiculoDAO() {
        emf = Persistence.createEntityManagerFactory("vehiculoPU");
        em = emf.createEntityManager();
    }
    
    
    public void closeEntityMAnager(){
        em.close();
    }
    
    public boolean insertarVehiculo(Vehiculo t){
        boolean b= false;
        try{
            em.getTransaction().begin();
            em.persist(t);
            em.getTransaction().commit();
            b=true;
        }catch(Exception e){
            em.getTransaction().rollback();
        }
        
        return b;
        
    }
    
    public boolean eliminarVehiculo(String id_vehivulo ){
        boolean b= false;
        try{
            em.getTransaction().begin();
            Vehiculo t = em.getReference(Vehiculo.class, id_vehivulo);
            em.remove(t);
            em.flush();
            em.getTransaction().commit();
            b=true;
        }catch(Exception e){
            em.getTransaction().rollback();
        }
        
        return b;
        
    }
    
    public List<Vehiculo> listarVehiculo(){
        return em.createQuery("SELECT t FROM Vehiculo t").getResultList();
            
    }
    

    public List<Cambio_Aceite> getListaCambio_Aceite() {
        return em.createQuery("SELECT c FROM Cambio_Aceite c").getResultList();   
    }
    
    public List<Vehiculo> getCambio_Aceite(String c) {
        System.out.println("codigo a buscar"+c);
        Query q = em.createQuery("SELECT t FROM Vehiculo t where t.cargoActual.Cambio_Aceite = : id_cambio_aceite");
        q.setParameter("id_cambio_aceite", c);
        return q.getResultList(); 
        
        
    }
}